package es.udc.emarketapp.http.controller.actions.products;

import es.udc.emarketapp.emarket.model.category.vo.CategoryVO;
import es.udc.emarketapp.emarket.model.categoryFacade.CategoryFacade;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;
import es.udc.emarketapp.emarket.model.product.vo.ProductVO;
import es.udc.emarketapp.emarket.model.productFacade.ProductFacade;
import es.udc.emarketapp.http.view.actionForms.ProductForm;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import javax.servlet.http.HttpSession;


public class EditProductDetailsAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException, InternalErrorException {

        ActionMessages errors = new ActionMessages();

        ProductForm productForm = (ProductForm) form;


        Long idProd = productForm.getIdProd();

        ProductFacade productFacade = new ProductFacade();
        ProductVO product = null;

        try {
            product = productFacade.findProductById(idProd);

            productForm.setIdCat(product.getIdCat());
            productForm.setName(product.getNomProd());
            productForm.setDescription(product.getDescripcion());
            DecimalFormatSymbols simbolos = new DecimalFormatSymbols();
            simbolos.setDecimalSeparator(',');
            DecimalFormat formateador = new DecimalFormat("####.####", simbolos);
            productForm.setPrice(formateador.format(product.getPrecio()));
            productForm.setWeight(formateador.format(product.getPeso()));
            productForm.setCountry(product.getPaisOrigen());
            productForm.setUds(Long.toString(product.getUdsDisp()));


         
            CountryFacade countryFacade = new CountryFacade();

            Locale locale = getLocale(request);
            List<CountryVO> countryVOs = countryFacade.findAllCountries(locale.getLanguage());
            request.setAttribute("countries", countryVOs);

            request.setAttribute("idProd", idProd);

            CategoryFacade categoryFacade = new CategoryFacade();
            HttpSession session = request.getSession(true);


            Long idCat = (Long)request.getAttribute("newCategory");

           
            if(request.getAttribute("newCategory") != null){
                 idCat = (Long)request.getAttribute("newCategory");
            }else{
                idCat = product.getIdCat();
            }

             request.setAttribute("idCat", idCat);
            

            try {
            /*path de categoria actual seleccionada*/
                List<CategoryVO> listCats = categoryFacade.getCategoryRoute(idCat);
            session.setAttribute("currentCategories", listCats);

            } catch (InstanceNotFoundException e) {
            errors.add("identifier",
                    new ActionMessage("ErrorMessages.category.notFound"));
            saveErrors(request, errors);
        }


        } catch (InstanceNotFoundException e) {
            errors.add("ProductIdentifier",
                    new ActionMessage("ErrorMessages.product.notFound"));
            saveErrors(request, errors);

            return new ActionForward(mapping.getInput());
        }

        return mapping.findForward("updateProductDetailsForm");
    }
}
